package Pixiv;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;


public class Pixiv {
	@SuppressWarnings("resource")
	public static void main(String[]args) throws Exception {
		String UserName;
		String PassWord;
		String SearchWord;
		int DayOrSearch;
		String SearchRealURL = null;
		String SearchURL="https://www.pixiv.net/search.php?s_mode=s_tag&word=";
		String DaryURL="https://www.pixiv.net/ranking.php?mode=daily&content=illust";
		String LoginURL="https://accounts.pixiv.net/api/login?lang=zh";
		String LoginGetPost_Key="https://accounts.pixiv.net/login?lang=zh&source=pc&view_type=page&ref=wwwtop_accounts_index";
		int p = 1;
		String SearchP="&order=date_d&p="+String.valueOf(p);
		List<String> list = new ArrayList<>();
		System.out.println("欢迎使用本程序，本程序所下载的图片保存在本程序所在的根目录下面的一个的新建文件夹内。");
		System.out.println("文件夹的名字为搜索词或每日排行，本程序只能下载jpg格式的图片png格式的图片无法下载——咸鱼");
		Scanner sacn = new Scanner(System.in);
		System.out.println("请输入你的Pixiv账号");
		UserName=sacn.nextLine();
		System.out.println("请输入密码");
		PassWord=sacn.nextLine();
		System.out.println("请输入你老婆的名字");
		SearchWord=sacn.nextLine();
		CloseableHttpClient closeableHttpClient=HttpClients.createDefault();//创建一个浏览器实例
		HttpGet loginget = new HttpGet(LoginGetPost_Key);
		HttpPost loginpost = new HttpPost(LoginURL);
		String post_key=null;
		try {
			CloseableHttpResponse loginGetpost_key=closeableHttpClient.execute(loginget);
			HttpEntity loginEnity=loginGetpost_key.getEntity();
			
			Post post = new Post();
			post.setPost_key(loginEnity);
			post_key=post.getPost_Key();
			post.setNvps(post_key, UserName, PassWord);
			loginpost.setEntity(new UrlEncodedFormEntity(post.getNvps(),"utf-8"));
		    CloseableHttpResponse LoginPosthttpResponse=closeableHttpClient.execute(loginpost);
	            if (LoginPosthttpResponse.getStatusLine().getStatusCode()==200) {
					System.out.println("登录成功");
				}else {
					System.out.println("登录失败");
				}//登录功能实现完毕
		System.out.println("请选择下载模式（1=下载每日插画排行榜图片，2=下载根据搜索词所搜索的图片）");
		DayOrSearch=sacn.nextInt();
		if (DayOrSearch==1) {
			DownLoad downLoad = new DownLoad();
			HttpGet SearchGet = new HttpGet(DaryURL);
			CloseableHttpResponse SearchGethttpResponse=closeableHttpClient.execute(SearchGet);
			Search search = new Search();
			list=search.getURL(SearchGethttpResponse,DayOrSearch);
			for (int i = 0; i < list.size(); i++) {
				String string=list.get(i);
				downLoad.newFile(string, "每日排行");
				Thread.sleep(2000);
			}
			
		} else {
			if (SearchWord!=null) {
				DownLoad downLoad = new DownLoad();
				for (int i = 1; i <=13; i++) {
					SearchRealURL=SearchURL+SearchWord+SearchP;
					HttpGet SearchGet = new HttpGet(SearchRealURL);
					CloseableHttpResponse SearchGethttpResponse=closeableHttpClient.execute(SearchGet);
					Search search = new Search();
					list=search.getURL(SearchGethttpResponse,DayOrSearch);
					for (int i1 = 0; i1 < list.size(); i1++) {
						String string=list.get(i1);
						downLoad.newFile(string,SearchWord);
						Thread.sleep(2000);
					}
					p++;
				}	
			} else {
				System.out.println("不能搜索空字符");
			}
		}
		}catch (Exception e) {
			// TODO: handle exception
		}finally {
			loginget.abort();
			loginpost.abort();//释放登录资源
		}
	}
}
